package org.bao.netcrawler;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bao.netcrawler.io.FileWriter;
import org.bao.netcrawler.net.Browser;
import org.bao.netcrawler.net.BrowserWithProxy;
import org.bao.netcrawler.utils.Configurer;
import org.bao.netcrawler.utils.HTMLFilter;
import org.bao.netcrawler.utils.Info;
import org.bao.netcrawler.policy.PolicyGen;
import org.bao.netcrawler.policy.model.Policy;
import org.bao.netcrawler.policy.model.Target;
/**
 * 
 * main logic
 * @author Bao<bb@feijiao.info>
 * @since 2010-12-24
 * @version 0.9.0
 * @license GPLv3
 */
public class Crawler{
	public static String format = null;
	
	public static Object[] crawl(String filepath){
		Configurer config = Configurer.getInstance();
		
		ArrayList<Policy> policies = PolicyGen.gen(filepath);
		
		//BAD DATA FILE
		if(policies == null){
			return null;
		}
		
		
		Browser browser = null;
		
		if(config.getCrawlerProxyEnable()){
			browser = new BrowserWithProxy();
		}
		else{
			browser = new Browser();
		}
		
		
		ArrayList<String> results = new ArrayList<String>();
		String format = "";
		
		for(int i = 0; i < policies.size(); i++){
			Policy policy = policies.get(i);
			ArrayList<Target> targets = policy.getTargets();
			
			format += (policies.get(i).getFormat() + "\n");
			
			//System.err.println("info@Crawler>" + policy.getUrl());
			String content = browser.getContent(policy.getUrl());
			
			//NETWORK NOT AVAILABLE
			if(content == null){
				return null;
			}
			
			String str = HTMLFilter.filter(content);
			
			for(int j = 0 ; j < targets.size(); j++){
				String substr = null;
				
				substr = str.split(targets.get(j).getPosition())[1];
				
				String reg = targets.get(j).getCharacter();
				Pattern pattern = null;
				Matcher matcher = null;
				
				pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
				matcher = pattern.matcher(substr);
				
				if(matcher.find()){
					results.add(matcher.group());
				}
				else{
					Info.type = Info.INFO_TYPE_NO_MATCH;
					return null;
				}
			}
		}
		
		Crawler.format = format;
		return results.toArray();
	}
	
	public static void out(String format, Object[] data){
		FileWriter.out(String.format(format, data).replaceAll("\n", " "));
	}
	
	public static void out(String path, String format, Object[] data){
		FileWriter.out(path, String.format(format, data));
	}
	
	/*
	public static void main(String[] args){
		//System.out.println(Crawler.crawl("./policies/policies.xml"));
		Object[] obj = Crawler.crawl("./policies/policies.xml");
		Crawler.out(Crawler.format, obj);
	}
	*/
}
